from django.shortcuts import render, redirect, HttpResponse
from web.forms import AuctionList, AuctionItemAdd, CouponList, CouponTitleForm
from api import models
from django.forms.models import model_to_dict
from django.http import JsonResponse
import datetime
from django.views.decorators.csrf import csrf_exempt
import uuid


def auction_data_view(request):
    """专场展示页面"""
    queryset_auction = models.Auction.objects.all()
    auction_list = queryset_auction
    return render(request, 'auction_list.html', context={'auction_list': auction_list})


def auction_add_view(request):
    """专场添加页面"""
    if request.method == 'GET':
        form = AuctionList()
        return render(request, 'auction_add_edit.html', context={'form': form})
    form = AuctionList(data=request.POST, files=request.FILES)
    if form.is_valid():
        form.save()
        return redirect('auction_list')
    print(form.errors)
    return render(request, 'auction_add_edit.html', context={'errors': form.errors})


def auction_edit_view(request, pk):
    """专场编辑页面"""
    old_auction_onj = models.Auction.objects.filter(pk=pk).first()
    if request.method == 'GET':
        form = AuctionList(instance=old_auction_onj)
        return render(request, 'auction_add_edit.html', context={'form': form})
    form = AuctionList(data=request.POST, instance=old_auction_onj, files=request.FILES)
    if form.is_valid():
        form.save()
        return redirect('auction_list')
    return render(request, 'auction_add_edit.html', context={'errors': form.errors})


def auction_del_view(request):
    """专场删除页面"""
    fid = request.GET.get('fid')
    if fid:
        # print(fid)
        models.Auction.objects.filter(pk=fid).delete()
        return JsonResponse({'status': 200})
    return JsonResponse({'status': 400})


def auction_item_data_view(request, pk):
    """拍卖商品列表"""
    # 拍卖专场消息
    auction_obj = models.Auction.objects.filter(pk=pk).first()
    # 拍卖品消息
    auction_item_queryset = models.AuctionItem.objects.filter(auction_id=pk)
    auction_item_list = auction_item_queryset
    # [
    #     model_to_dict(item, fields=['id', 'uid', 'title', 'cover', 'start_price', 'deal_price', 'reserve_price',
    #                                 'highest_price', 'unit', 'status'])
    #     for item in
    #     auction_item_queryset]
    return render(request, 'auction_item_list.html',
                  context={'auction_item_list': auction_item_list, 'auction_obj': auction_obj})


def auction_item_add_view(request, pk):
    """添加拍品"""
    auction_object = models.Auction.objects.filter(id=pk).first()
    if request.method == 'GET':
        form = AuctionItemAdd()
        return render(request, 'auction_item_add.html', context={'form': form, 'auction_object': auction_object})
    form = AuctionItemAdd(data=request.POST, files=request.FILES)
    if form.is_valid():
        # 保存之前，需要把用户不用传的数据，填上
        form.instance.auction = auction_object
        form.instance.uid = datetime.datetime.now().strftime('%Y%m%d%H%M%S%f')
        form.save()
        return JsonResponse({'status': 201})
    return JsonResponse({'status': 400})


def coupon_list(request):
    """优惠劵展示"""
    coupon_query = models.CouponStrength.objects.filter(is_show=True, is_delete=False)

    return render(request, 'coupon_list.html', context={'coupon_query': coupon_query})


def coupon_add_view(request):
    """优惠劵添加页面"""
    if request.method == 'GET':
        form = CouponList()
        return render(request, 'coupon_add_edit.html', context={'form': form})
    form = CouponList(data=request.POST, files=request.FILES)
    # print(request.POST, request.FILES)
    if form.is_valid():
        form.save()
        return redirect('coupon_list')
    print(form.errors)
    return render(request, 'coupon_add_edit.html', context={'errors': form.errors})


def coupon_edit_view(request, pk):
    """优惠劵编辑页面"""
    old_coupon_onj = models.CouponStrength.objects.filter(pk=pk).first()
    if request.method == 'GET':
        form = CouponList(instance=old_coupon_onj)
        return render(request, 'coupon_add_edit.html', context={'form': form})
    form = CouponList(data=request.POST, instance=old_coupon_onj, files=request.FILES)
    if form.is_valid():
        form.save()
        return redirect('coupon_list')
    return render(request, 'coupon_add_edit.html', context={'errors': form.errors})


def coupon_del_view(request):
    """优惠劵删除页面"""
    fid = request.GET.get('fid')
    if fid:
        # print(fid)
        models.CouponStrength.objects.filter(pk=fid).update(is_delete=True)
        return JsonResponse({'status': 200})
    return JsonResponse({'status': 400})


def coupon_title(request):
    """优惠劵名称"""
    coupon_title_query = models.CouponTitle.objects.filter(is_show=True, is_delete=False)

    return render(request, 'coupon_title_list.html', context={'coupon_title_query': coupon_title_query})


def coupon_title_add_view(request):
    """活动添加"""
    if request.method == 'GET':
        form = CouponTitleForm()
        return render(request, 'title_add_edit.html', context={'form': form})
    form = CouponTitleForm(data=request.POST)
    if form.is_valid():
        form.save()
        return redirect('coupon_title_list')
    print(form.errors)
    return render(request, 'title_add_edit.html', context={'errors': form.errors})


def coupon_title_edit_view(request, pk):
    """活动编辑"""
    old_coupon_title = models.CouponTitle.objects.filter(pk=pk).first()
    if request.method == 'GET':
        form = CouponTitleForm(instance=old_coupon_title)
        return render(request, 'title_add_edit.html', context={'form': form})
    form = CouponTitleForm(data=request.POST, instance=old_coupon_title, files=request.FILES)
    if form.is_valid():
        form.save()
        return redirect('coupon_title_list')
    return render(request, 'title_add_edit.html', context={'errors': form.errors})


def coupon_title_del_view(request):
    """优惠劵删除页面"""
    fid = request.GET.get('fid')
    if fid:
        # print(fid)
        models.CouponTitle.objects.filter(pk=fid).update(is_delete=True)
        return JsonResponse({'status': 200})
    return JsonResponse({'status': 400})
